package com.example.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mapper.master.OrderMapper;
import com.example.model.Order;
import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author long
 * @date 2024/12/11 14:46
 */
@Slf4j
@Service
//@EnableScheduling
public class OrderPayTimeOutScanService{

    @Resource
    private OrderMapper orderMapper;

    @Scheduled(cron = "*/5 * * * * ?")
    public void scanOrder() {

        log.info("开始扫描未支付订单列表");

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Order::getValid,1) // 有效
                .eq(Order::getPay,0); // 未支付
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(orderList)){
            return;
        }

        // 遍历未支付订单，判断是否超时
        orderList.forEach(order->{
            // 截止时间 < 当前时间
            if(order.getDeadline().before(new Date())){
                // 订单置为失效
                order.setValid(0);
                orderMapper.updateById(order);
                log.info("订单:{}已经置为失效",order.getId());
            }
        });


    }

}
